package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Model;
import model.Sip;
import util.Util;

/**
 * Servlet implementation class SipController
 */
public class SipController extends Controller {
	private static final long serialVersionUID = 1L;
	private final String KEY = "sip";
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SipController() {
        super();
    }
    
    protected Class<?> getClassModel() {
    	return Sip.class;
    }
    
    protected String getPathConfig() {
    	return Util.getPath(this.KEY);
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		RequestDispatcher rd = request.getRequestDispatcher("sip.jsp");
		
		String act = request.getParameter("act") == null ? "" : request.getParameter("act");
		
		if (act.matches("insertUpdate")) {
			this.create(request, response);
			
			request.setAttribute("act", "insertUpdate");
			
		} else if (act.matches("retrieve")) {
			Model model = this.retrieve(request, response);
			
			request.setAttribute("obj", model);
			
			request.setAttribute("act", "insertUpdate");
			
		} else if (act.matches("update")) {
			this.update(request, response);
			
			request.setAttribute("act", "insertUpdate");
			
		} else if (act.matches("delete")) {
			this.delete(request, response);
			
			request.setAttribute("act", "insertUpdate");
			
		} else {
			
			request.setAttribute("act", "insertUpdate");
		}
		
		ArrayList<Model> lista = listConfig();
		
		request.setAttribute("lista", lista);
		
		rd.forward(request, response);
	}

}
